/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sopraim.presenter.validate;

import sopraim.model.Model;

/**
 *
 * @author justeabel
 */
public class AddUserForm {
    
    public  AddUserForm (String admin, String username, String email) throws Exception {
        validateUsername(username);
        validateEmail(email);
        validateAdmin(admin);
    }
    
    public  AddUserForm (String admin, String username, String name, String firstName, String email) throws Exception {
        validateUsername(username);
        validateUsername(name);
        validateUsername(firstName);
        validateEmail(email);
        validateAdmin(admin);
    }
    private void validateAdmin(String admin) throws Exception
    {
        if(admin == null) 
            throw(new Exception("Error : admin is empty"));
        if (admin.trim().isEmpty())
            throw(new Exception("Error : admin is only composed of space"));
        if (!Model.getInstance().getUserDAO().findByLogin(admin).getIsAdmin())
            throw(new Exception("Error : you do not have the admin rights"));
    }
    private void validateUsername(String username) throws Exception
    {
        if(username == null) 
            throw(new Exception("Error : username is empty"));
        if (username.trim().isEmpty())
            throw(new Exception("Error : username is only composed of space"));
    }
    
    private void validateEmail(String email) throws Exception
    {
        if(email == null) 
            throw(new Exception("Error : email is empty"));
        if (email.trim().isEmpty())
            throw(new Exception("Error : email is only composed of space"));
    }
}
               